package com.atguigu.lease.web.admin.controller.lease;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.atguigu.lease.web.admin.vo.agreement.AgreementVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;


@Tag(name = "租约管理")
@RestController
@RequestMapping("/admin/agreement")
public class LeaseAgreementController {
    @Autowired
    private LeaseAgreementService leaseAgreementService;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private RoomInfoService roomInfoService;
    @Autowired
     private PaymentTypeService paymentTypeService;
    @Autowired
    private LeaseTermService leaseTermService;

    /**
     * 保存或修改租约信息
     * @param leaseAgreement
     * @return
     */
    @Operation(summary = "保存或修改租约信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody LeaseAgreement leaseAgreement) {
        leaseAgreementService.saveOrUpdate(leaseAgreement);
        return Result.ok();
    }

    /**
     * 根据条件分页查询租约列表
     * @param current
     * @param size
     * @param queryVo
     * @return
     */
    @Operation(summary = "根据条件分页查询租约列表")
    @GetMapping("page")
    public Result<IPage<AgreementVo>> page(@RequestParam long current, @RequestParam long size, AgreementQueryVo queryVo) {
        Page<AgreementVo> page=new Page<>();
        leaseAgreementService.customPage(page,queryVo);
        return Result.ok(page);
    }

    /**
     * 根据id查询租约信息
     * 原先逻辑有问题，相关业务应该在service层实现
     * @param id
     * @return
     */
    @Operation(summary = "根据id查询租约信息")
    @GetMapping(name = "getById")
    public Result<AgreementVo> getById(@RequestParam Long id) {
        AgreementVo agreementVo=leaseAgreementService.customGetById(id);
        return Result.ok(agreementVo);
    }

    /**
     * 根据id删除租约信息，如果在租约状态，则不能删除
     * @param id
     * @return
     */
    @Operation(summary = "根据id删除租约信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {
        leaseAgreementService.customRemoveById(id);
        return Result.ok();
    }

    /**
     * 根据id更新租约状态
     * @param id
     * @param status
     * @return
     */
    @Operation(summary = "根据id更新租约状态")
    @PostMapping("updateStatusById")
    public Result updateStatusById(@RequestParam Long id, @RequestParam LeaseStatus status) {
        LeaseAgreement leaseAgreement = new LeaseAgreement();
        leaseAgreement.setId(id);
        leaseAgreement.setStatus(status);
        leaseAgreementService.updateById(leaseAgreement);
        return Result.ok();
    }

}

